<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>人口动态信息管理平台登录</title>
    <link rel="stylesheet" href="__PUBLIC__/frame/layui/css/layui.css">
    <link rel="stylesheet" href="__PUBLIC__/frame/static/css/style.css">
    <link rel="icon" href="__PUBLIC__/frame/static/image/code.png">
</head>
<body class="login-body body">

<div class="login-box">
    <form class="layui-form layui-form-pane" method="get" action="">
        <div class="layui-form-item">
            <h3>人口动态信息管理平台登录</h3>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">账号：</label>

            <div class="layui-input-inline">
                <input type="text" name="username" id="username" class="layui-input"  placeholder="用户名"
                       autocomplete="off" maxlength="20"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码：</label>

            <div class="layui-input-inline">
                <input type="password" id="password" name="password" class="layui-input" placeholder="密码"
                       maxlength="20"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="float: left">验证码：</label>

            <div class="layui-input-inline">
                <input style="width: 80px;" id="yzm" type="text" name="code" class="layui-input" placeholder="验证码" maxlength="4"/><img style="position: absolute;top: 0;left: 80px;width: 100px" src="{:U('Login/vcode')}" id="vcode" title="看不清,点击我换一张"><a style="position: relative;left: 100px;font-size: 12px" id="kanbuq" href="javascript:;">看不清，换一张</a>
            </div>
        </div>
        <div class="layui-form-item">
            <button type="reset" class="layui-btn layui-btn-danger btn-reset">重置</button>
            <button type="button" class="layui-btn btn-submit">立即登录</button>
        </div>
    </form>
</div>

<script type="text/javascript" src="__PUBLIC__/frame/layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['form', 'layer'], function () {

        // 操作对象
        var form = layui.form
                , layer = layui.layer
                , $ = layui.jquery;

        //点击切换验证码
        $('#kanbuq').add('#vcode').click(function () {
            $('#vcode')[0].src = "__CONTROLLER__/vcode/" + Math.random();
        });


        $('.btn-submit').click(function () {
            var vcode = $("#yzm").val();
            var username = $("#username").val();
            var password = $("#password").val();
            if(!vcode) {
                return layer.msg("验证码不能为空",{icon:0});
            }
            if(!password) {
                return layer.msg("密码不能为空",{icon:0});
            }
            if(!username) {
                return layer.msg("用户名不能为空",{icon:0});
            }
            $.ajax({
                type: 'post',
                url: "__CONTROLLER__/yzm",
                data: "yzm=" + vcode,
                dataType: 'text',    //请求头信息
                success: function (data) {
                    if(data == 'false') {
//                        console.log(typeof data);
                        $('#vcode')[0].src = "__CONTROLLER__/vcode/" + Math.random();//当有错误时刷新一下验证码
                        $("#yzm").val('');  //将验证码置空
                        return layer.msg('验证码错误',{icon: 5});
                    } else {   //验证码正确后
                        var url = "/index.php/Home/Login/check";
                        var meg = {'username': username, 'password': password};
                        $.post(url, meg, function (result) {
//                            console.log(typeof result);   //object
                            if(result.status == 0) {
                                $('#vcode')[0].src = "__CONTROLLER__/vcode/" + Math.random();//当有错误时刷新一下验证码
                                $("#yzm").val('');  //将验证码置空
//                                return dialog.error(result.message);
                                return layer.msg(result.message,{icon: 5});
                            }
                            if(result.status == 1) {
                                layer.msg(result.message,{icon: 1});
                                window.location.href = "/index.php/Home/Index/index";
//                                return dialog.success(result.message, );//如果成功就跳到首页
                            }
                        }, "JSON"); //这里的json一定要加上引号,不然接收不到数据
                    }
                }
            });
        });


        // 键盘回车发送数据
        $(document).keyup(function(event){
            if(event.keyCode ==13){
                $(".btn-submit").trigger("click");
            }
        });

    })


</script>
</body>
</html>